#!/bin/bash

# APK Resign Tool - APK签名模块
# 负责签名APK文件和重签名功能

# 从其他模块导入函数
if [ -z "$(type -t run_build_tool)" ]; then
    source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
fi
if [ -z "$(type -t unpack_apk)" ]; then
    source "$(dirname "${BASH_SOURCE[0]}")/unpack.sh"
fi
if [ -z "$(type -t build_apk)" ]; then
    source "$(dirname "${BASH_SOURCE[0]}")/build.sh"
fi

# 签名APK文件
sign_apk() {
    local unsigned_apk="$1"
    local keystore_file="$2"
    local alias_name="${3:-my-key-alias}"
    local storepass="${4:-android}"
    local keypass="${5:-android}"
    local output_apk="${unsigned_apk%.*}.signed.apk"
    
    if [ ! -f "$unsigned_apk" ]; then
        echo "错误：未签名APK不存在：$unsigned_apk"
        return 1
    fi
    
    if [ ! -f "$keystore_file" ]; then
        echo "错误：密钥库文件不存在：$keystore_file"
        return 1
    fi
    
    # 创建临时对齐APK
    local aligned_apk="$(mktemp).apk"
    
    # 对齐APK（Android 11+需要）
    echo "正在对齐APK..."
    run_build_tool zipalign -p 4 "$unsigned_apk" "$aligned_apk"
    
    if [ $? -ne 0 ]; then
        echo "对齐APK失败"
        rm -f "$aligned_apk"
        return 1
    fi
    
    # 使用apksigner签名APK
    echo "正在使用密钥库 $keystore_file 通过apksigner签名APK..."
    echo "apksigner参数:"
    echo "  密钥库文件: $keystore_file"
    echo "  密钥库密码: $storepass"
    echo "  密钥密码: $keypass" 
    echo "  密钥别名: $alias_name"
    echo "  输出文件: $output_apk"
    echo "  输入文件: $aligned_apk"
    
    run_build_tool apksigner sign --ks "$keystore_file" \
        --ks-pass pass:"$storepass" \
        --key-pass pass:"$keypass" \
        --ks-key-alias "$alias_name" \
        --out "$output_apk" \
        "$aligned_apk"
    
    if [ $? -ne 0 ]; then
        echo "签名APK失败"
        rm -f "$aligned_apk"
        return 1
    fi
    
    # 清理
    rm -f "$aligned_apk"
    
    echo "成功签名APK：$output_apk"
    return 0
}

# 重签名APK文件（解包、构建、签名）
resign_apk() {
    local apk_file="$1"
    local keystore_file="$2"
    local alias_name="${3:-my-key-alias}"
    local storepass="${4:-android}"
    local keypass="${5:-android}"
    
    if [ ! -f "$apk_file" ]; then
        echo "错误：APK文件不存在：$apk_file"
        return 1
    fi
    
    if [ ! -f "$keystore_file" ]; then
        echo "错误：密钥库文件不存在：$keystore_file"
        return 1
    fi
    
    # 创建临时目录用于解包APK
    local temp_dir="$(mktemp -d)"
    local base_name="$(basename "$apk_file" .apk)"
    local unsigned_apk="$temp_dir/$base_name.unsigned.apk"
    local output_apk="${apk_file%.*}.resigned.apk"
    
    # 解包APK
    echo "正在解包APK..."
    unpack_apk "$apk_file" "$temp_dir/$base_name"
    
    if [ $? -ne 0 ]; then
        echo "解包APK失败"
        rm -rf "$temp_dir"
        return 1
    fi
    
    # 构建APK
    echo "正在构建APK..."
    build_apk "$temp_dir/$base_name" "$unsigned_apk"
    
    if [ $? -ne 0 ]; then
        echo "构建APK失败"
        rm -rf "$temp_dir"
        return 1
    fi
    
    # 创建临时对齐APK
    local aligned_apk="$(mktemp).apk"
    
    # 对齐APK（Android 11+需要）
    echo "正在对齐APK..."
    run_build_tool zipalign -p 4 "$unsigned_apk" "$aligned_apk"
    
    if [ $? -ne 0 ]; then
        echo "对齐APK失败"
        rm -rf "$temp_dir" "$aligned_apk"
        return 1
    fi
    
    # 使用apksigner签名APK
    echo "正在使用密钥库 $keystore_file 通过apksigner签名APK..."
    run_build_tool apksigner sign --ks "$keystore_file" \
        --ks-pass pass:"$storepass" \
        --key-pass pass:"$keypass" \
        --ks-key-alias "$alias_name" \
        --out "$output_apk" \
        "$aligned_apk"
    
    if [ $? -ne 0 ]; then
        echo "签名APK失败"
        rm -rf "$temp_dir" "$aligned_apk"
        return 1
    fi
    
    # 清理
    rm -rf "$temp_dir" "$aligned_apk"
    
    echo "成功重签名APK：$output_apk"
    return 0
} 